﻿using CBS.Common;
using CBS.DB.CBSEntity;
using CBS.DB;
using CBS.Model.ManageModel;
using CBS.Model.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace CBS.APP
{
    public class CourseApp
    {
        public static bool CreateCourse(CourseViewModel newCourse)
        {
            using (CBSContext cbs = new CBSContext())
            {
                cbs.cbs_course.Add(new cbs_course
                {
                    CourseName = newCourse.CourseName,
                });

                return cbs.SaveChanges() > 0;
            }
        }

        public static List<CourseViewModel> GetList(Pagination page, string? keyword)
        {
            if (string.IsNullOrWhiteSpace(keyword))
            {
                return GetList(null, page);
            }
            else
            {
                return GetList(t => t.CourseName.Contains(keyword), page);
            }

        }

        public static List<CourseViewModel> GetList(Expression<Func<cbs_course, bool>>? predicate, Pagination page)
        {
            List<cbs_course> list;
            RepositoryBase<cbs_course> repo = new RepositoryBase<cbs_course>();
            if (predicate == null)
            {
                list = repo.FindList(page);
            }
            else
            {
                list = repo.FindList(predicate, page);
            }


            return PropertyHelper.Assignment<CourseViewModel, cbs_course>(list);
        }

        public static List<CourseViewModel> GetSelectList()
        {
            CBSContext cbs = new();
            var data = (from cou in cbs.cbs_course
                        select new CourseViewModel
                        {
                            CourseId = cou.CourseId,
                            CourseName = cou.CourseName
                        }).ToList();
            if (data != null)
            {
                return data;
            }
            else 
            {
                return new List<CourseViewModel>(); 
            }
        }

        public static List<CourseViewModel> GetSelectList(int semesterId)
        {
            CBSContext cbs = new();
            return (from course in cbs.cbs_course_plan
                    join cou in cbs.cbs_course on course.CourseId equals cou.CourseId
                    where course.SemesterId == semesterId
                    select new CourseViewModel
                    {
                        CourseId = cou.CourseId,
                        CourseName = cou.CourseName
                    }).ToList();
        }

        public static bool DeleteCourse(int courseId, out string msg)
        {
            msg = "";
            using (CBSContext cbs = new CBSContext())
            {
                if (cbs.cbs_course_plan.Any(t=>t.CourseId == courseId))
                {
                    msg = "数据已经被使用，不能删除。";
                    return false;
                }
                cbs.cbs_course.Remove(cbs.cbs_course.First(t => t.CourseId == courseId));

                return cbs.SaveChanges() > 0;
            }
        }

        public static bool BatchDeleteCourse(string key)
        {
            using (CBSContext cbs = new CBSContext())
            {
                cbs.cbs_course.RemoveRange(cbs.cbs_course.Where(t => t.CourseName.Contains(key)));
                return cbs.SaveChanges() > 0;
            }
        }

        public static CourseViewModel GetForm(int courseId)
        {
            using (CBSContext cbs = new CBSContext())
            {
                var entity = cbs.cbs_course.FirstOrDefault(t => t.CourseId == courseId);

                return PropertyHelper.Assignment<CourseViewModel, cbs_course>(entity);
            }
        }
    }
}
